﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.Sys.Database;

namespace UFIDA.U9.CustDev.HL.BEPlugIns
{
    public class Dbops
    {
        private StringBuilder sqltxt = new StringBuilder();
        private DataParamList dpl = new DataParamList();
        //获取料品在手量 pcs
        public decimal WhQuery(string itemid,string whid)
        {
            decimal result;
            sqltxt.Length = 0;
            sqltxt.Append(" select sum((A.[StoreMainQty] + A.[ToRetStMainQty])) as [BalQty_Main] ");
            sqltxt.Append(" from  InvTrans_WhQoh as A ");
            sqltxt.Append(" where A.ItemInfo_ItemID=" + itemid + " and A.Wh=" + whid);
            DataSet ds = new DataSet();
            DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl, out ds);
            try
            {
                result = decimal.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
            catch {
                result = 0;
            }
            return result;
        }
        //设置料品单位重量
        public void setItemWeight(string itemid, string whid, string weight, decimal itemqty,int control)//control为0则为审核，1则为弃申
        {
            try
            {
                weight = (decimal.Parse(weight) * 1000).ToString();
            }
            catch {
                weight = "0";
            }
            sqltxt.Length = 0;
            sqltxt.Append(" EXEC [dbo].[CreateOrUpdateWeight] " + getID(itemid, whid) + "," + itemid + "," + whid + "," + weight.ToString() + "," + itemqty.ToString() + ",'" + DateTime.Now.ToString() + "'," + control.ToString());
            DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl);
        }

        //写入异动明细表
        public void IntoTrans(string docid, string lineid, string weight)
        {
            if (string.IsNullOrEmpty(weight))
                weight = "0";
            sqltxt.Length = 0;
            //sqltxt.Append("update InvTrans_TransLine set DescFlexField_PubDescSeg8=" + weight.ToString() + " where Doc_EntityID=" + docid + " and DocLine_EntityID=" + lineid);
            //sqltxt.Append("update InvTrans_TransLine_Trl set DescFlexField_CombineName=dbo.F_SetCombineName((select top 1 ID from InvTrans_TransLine where Doc_EntityID=" + docid + " and DocLine_EntityID=" + lineid + ")) where ID=((select top 1 ID from InvTrans_TransLine where Doc_EntityID=" + docid + " and DocLine_EntityID=" + lineid + "))");
            //DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl);
        }

        public void IntoTransSub(string docid, string lineid ,string linesubid, string weight)
        {
            if (string.IsNullOrEmpty(weight))
                weight = "0";
            sqltxt.Length = 0;
            //sqltxt.Append("update InvTrans_TransLine set DescFlexField_PubDescSeg8=" + weight.ToString() + " where Doc_EntityID=" + docid + " and DocLine_EntityID=" + lineid + " and DocSubLine_EntityID=" + linesubid);
            //sqltxt.Append("update InvTrans_TransLine_Trl set DescFlexField_CombineName=dbo.F_SetCombineName((select top 1 ID from InvTrans_TransLine where Doc_EntityID=" + docid + " and DocLine_EntityID=" + lineid + " and DocSubLine_EntityID=" + linesubid + ")) where ID=((select top 1 ID from InvTrans_TransLine where Doc_EntityID=" + docid + " and DocLine_EntityID=" + lineid + " and DocSubLine_EntityID=" + linesubid + "))");
            //DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl);
        }

        //生成ID
        private string getID(string itemid, string whid)
        {
            string id = "";
            if ((itemid.Length == 16) && (whid.Length == 16))
                id = itemid.Substring(9, 7) + whid.Substring(9, 7) + DateTime.Now.Millisecond.ToString();
            return id;
        }

        //获取料品单位重量 G
        public decimal getItemWeight(string itemid,string whid)
        {
            string itemWeight = "0";
            DataSet ds1 = new DataSet();
            sqltxt.Length = 0;
            sqltxt.Append("select top 1 coalesce(Weight,0) from CBO_ItemWeight where ItemInfo_ItemID=" + itemid + " and Wh=" + whid);
            DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl, out ds1);
            if (ds1.Tables[0].Rows.Count > 0)
                itemWeight = ds1.Tables[0].Rows[0][0].ToString() ?? "0";
            try { return decimal.Parse(itemWeight); }
            catch { return 0; }
        }

        //获取料品标准单重
        public decimal getItemStandWeight(string itemid)
        {
            string itemWeight = "0";
            DataSet ds1 = new DataSet();
            sqltxt.Length = 0;
            sqltxt.Append("select top 1 coalesce(Weight,0) from CBO_ItemMaster where ID=" + itemid);
            DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl, out ds1);
            if (ds1.Tables[0].Rows.Count > 0)
                itemWeight = ds1.Tables[0].Rows[0][0].ToString() ?? "0";
            try { return decimal.Parse(itemWeight); }
            catch { return 0; }
        }

        //获取来源仓库ID
        public string GetSrcWhID(string itemid, string so, int type) //type: 0为杂收单 1为生产退料单
        {
            if ((string.IsNullOrEmpty(itemid)) || (string.IsNullOrEmpty(so)))
                return "-1";
            string s;
            s = "";
            DataParamList dpl2 = new DataParamList();
            DataSet ds2 = new DataSet();
            if (type == 0)
                s = "select top 1 A.Wh from InvDoc_MiscShipL as A where A.ItemInfo_ItemID=" + itemid + " and A.DescFlexSegments_PubDescSeg12='" + so + "'";
            if (type == 1)
                s = "select top 1 Wh from MO_IssueDocLine where IssueDoc in (select B.ID from MO_IssueDoc as B where B.IssueType in (0,3) and B.DocState in (1,2)) and Item=" + itemid + " and MO=" + so;
            DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), s, dpl2, out ds2);
            if (ds2.Tables[0].Rows.Count > 0)
                return ds2.Tables[0].Rows[0][0].ToString();
            else
                return "-1";
        }


        #region 替换弹性域和并列值
        //替换
        public string segvalue(string combinevlaue, string newvalue, int seq)
        {
            Hashtable hs = new Hashtable();
            string result;
            result = "";
            int i = 0;
            while (combinevlaue.Contains("#@#"))
            {
                i = i + 1;
                hs.Add(i.ToString(), combinevlaue.Substring(0, combinevlaue.IndexOf("#@#") + 3));
                combinevlaue = combinevlaue.Remove(0, combinevlaue.IndexOf("#@#") + 3);
            }
            hs[seq.ToString()] = newvalue + "#@#";
            for (int n = 1; n <= i; n++)
            {
                result = result + hs[n.ToString()].ToString();
            }
            return result;
        }
        #endregion

        #region 值写入
        public void writeData(string LineID, string Value, string TableName, string ColName)
        {
            sqltxt.Length = 0;
            sqltxt.Append("update "+TableName+" set "+ColName+"='"+Value+"' where ID="+LineID);
            DataAccessor.RunSQL(DatabaseManager.GetCurrentConnection(), sqltxt.ToString(), dpl);
        }
        #endregion

        #region 单重浮动比例合法检测
        public bool isRight(decimal docweight,string itemid,string whid)
        {
            decimal rate = 0.5M;
            decimal standweight = this.getItemStandWeight(itemid);
            if(standweight.Equals(0))
                return true;
            if ((docweight > (standweight * (1 + rate))) || (docweight < (standweight * (1 - rate))))
                return false;
            return true;
        }
        #endregion
    }
}
